//
//  UIImage+WebCache.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/10/14.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "UIImage+WebCache.h"

@implementation UIImage_WebCache

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)setImageWithURL:(NSURL *)url{
    
    
    
    //第一种方法：普通的加载
    
    /*
     
     NSData *data = [NSData dataWithContentsOfURL:url];
     
     UIImage *image = [UIImage imageWithData:data];
     
     [self setImage:image];
     
     */
    
    
    
    //第二种方法：线程
    
    //NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImage:) object:url];
    
    
    
    //[thread start];
    
    
    
    //第三种方法：队列
    
    /* NSOperationQueue *queue = [[NSOperationQueue alloc] init];
     
     //操作数
     
     NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
     
     
     
     NSData *data = [NSData dataWithContentsOfURL:url];
     
     UIImage *image = [UIImage imageWithData:data];
     
     
     
     //使用主队列来回到主线程
     
     //在主线程上修改UI
     
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
     
     //修改UI界面
     
     [self setImage:image];
     
     }];
     
     }];
     
     
     
     [queue addOperation:op];
     
     */
    
    
    
    //----------------GCD---------------
    
    // //1.创建Queue
    
    // dispatch_queue_t queue = dispatch_queue_create("com.xw.gcd.queue", DISPATCH_QUEUE_CONCURRENT);//并行队列
    
    //
    
    // //2.创建要执行的任务，加到队列中
    
    // dispatch_async(queue, ^{
    
    //
    
    // NSData *data = [NSData dataWithContentsOfURL:url];
    
    // UIImage *image = [UIImage imageWithData:data];
    
    // [self setImage:image];
    
    // });
    
    
    
    
    
    //用系统给的queue---
    
    // (两个参数)
    
    //参数1：优先级
    
    //参数2：标识符
    
    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    
    
    dispatch_async(globalQueue, ^{
        
        //请求数据
        
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        UIImage *image = [UIImage imageWithData:data];
        
        
        
        //显示在UI界面上
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //UI相关的代码
            
            //UI界面的刷新最好回到主线程中
            
            [self setImage:image];
            
        });
        
    });
    
    
    
}



- (void)loadImage:(NSURL *)url{
    
   // NSData *data = [NSData dataWithContentsOfURL:url];
    
   // UIImage *image = [UIImage imageWithData:data];
    
   // [self setImage:image];
    
    
    [self setImageWithURL:url];
    
    
}

@end
